// 发表评论
function addComment(content, postId = null, movieId = null, musicId = null, parentId = null) {
    fetch('/api/comments', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            content: content,
            post_id: postId,
            movie_id: movieId,
            music_id: musicId,
            parent_id: parentId
        })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert(data.message);
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('评论失败，请重试');
    });
}

// 删除评论
function deleteComment(commentId) {
    if (!confirm('确定要删除这条评论吗？')) {
        return;
    }
    
    fetch(`/api/comments/${commentId}`, {
        method: 'DELETE'
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert(data.message);
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('删除失败，请重试');
    });
}

// 添加事件监听器
document.addEventListener('DOMContentLoaded', function() {
    const commentForm = document.querySelector('.comment-form');
    if (commentForm) {
        const textarea = commentForm.querySelector('textarea');
        const submitBtn = commentForm.querySelector('button');
        
        submitBtn.addEventListener('click', function() {
            const content = textarea.value.trim();
            if (!content) return;
            
            // 获取当前页面的文章/电影/音乐ID
            const postId = document.querySelector('[data-post-id]')?.dataset.postId;
            const movieId = document.querySelector('[data-movie-id]')?.dataset.movieId;
            const musicId = document.querySelector('[data-music-id]')?.dataset.musicId;
            
            addComment(content, postId, movieId, musicId);
        });
    }
    
    // 绑定回复按钮事件
    document.querySelectorAll('.reply-btn').forEach(btn => {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            const commentItem = this.closest('.comment-item');
            const replyForm = commentItem.querySelector('.reply-form');
            
            if (replyForm) {
                replyForm.remove();
            } else {
                const form = document.createElement('div');
                form.className = 'reply-form mt-2';
                form.innerHTML = `
                    <div class="input-group">
                        <textarea class="form-control" rows="2" placeholder="写下你的回复..."></textarea>
                        <div class="input-group-append">
                            <button class="btn btn-primary btn-sm">回复</button>
                        </div>
                    </div>
                `;
                
                const parentId = commentItem.dataset.commentId;
                form.querySelector('button').addEventListener('click', function() {
                    const content = form.querySelector('textarea').value.trim();
                    if (!content) return;
                    
                    const postId = document.querySelector('[data-post-id]')?.dataset.postId;
                    const movieId = document.querySelector('[data-movie-id]')?.dataset.movieId;
                    const musicId = document.querySelector('[data-music-id]')?.dataset.musicId;
                    
                    addComment(content, postId, movieId, musicId, parentId);
                });
                
                commentItem.appendChild(form);
            }
        });
    });
    
    // 绑定删除按钮事件
    document.querySelectorAll('.delete-btn').forEach(btn => {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            const commentId = this.closest('.comment-item').dataset.commentId;
            deleteComment(commentId);
        });
    });
}); 